﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class AB45AV95 : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI
        {
            get
            {
                return (INTEXDataSet)TryFindResource("iNTEXDataSet");
            }
        }
        public INTEXDataSet setWorker = new INTEXDataSet();
        AB45AV95Data m_Data, m_DataX;
        AB45AV95Report m_rpt;
        public DataSet setResult;
        
        private DataRow dwUIABPOS;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet();
        public AB45AV95()
        {
            InitializeComponent();
            this.Name = "浆纱生产状态报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];          
            m_Data = new AB45AV95Data(OracleDB,proxy);           
        }
        #endregion
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();

                dwUIABPOS["ABPAUFART"] = "VW";
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();
                string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
                if (ERLKZ == "True")
                {
                    dwUIABPOS["ABPERLKZ"] = "J";
                }
                else
                {
                    dwUIABPOS["ABPERLKZ"] = " ";
                }

                dwUIABPOS["ABPAUFKRS"] = KRS;
                dwUIABPOS["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUIABPOS["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }

                dwUIABPOS["ABPZUGET"] = "0";
                dwUIABPOS["ABPART"] = txtArticle.Text.ToString().Trim();
                
                setResult = m_Data.LoadABPOS(dwUIABPOS);
                setUI.Clear();
                setUI.Merge(setResult);

                totalrecord = setUI.ABPOS.Rows.Count;
                foreach (DataRow dwNABPOS in setUI.ABPOS.Rows)
                { 
                    ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage("AB45AV95Data", dwNABPOS, OracleDB, proxy,setUI);
                }
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUIABPOS = setUI.ABPOS.NewRow();
                if (setUI != null)
                {
                    //setResult = m_Data.GetWASTAMM();
                    //setUI.Merge(setResult);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        public void GenerateReport(string PrintType)
        {
            Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

            // check to kill the right process
            /*foreach (Process ExcelProcess in AllProcesses)
            {
                ExcelProcess.Kill();
            }*/
           /* using (new LongOperation(this, "读取符合条件的记录"))
            {
                if (setANINFOR.Tables.Contains("ABPOS"))
                {
                    setANINFOR.Tables["ABPOS"].Clear();
                }
                string VWORDERNUM = "";
                DataRow dwABINFO = null;
                bool flgNew = false;
                foreach (DataRow dwTemp in setUI.Tables[0].Rows)
                {
                    VWORDERNUM = dwTemp["VWORDERNUM"].ToString();
                    var query = from DataRow dwExist in setANINFOR.Tables[0].AsEnumerable()
                                where dwExist["VWORDERNUM"].ToString() == VWORDERNUM
                                select dwExist;
                    if (query.Count() > 0)
                    {
                        foreach (DataRow dwExist in query)
                        {
                            dwABINFO = dwExist;
                        }
                    }
                    else
                    {
                        dwABINFO = setANINFOR.Tables["ABPOS"].NewRow();
                        flgNew = true;
                    }
                    foreach (DataColumn dcTemp in dwABINFO.Table.Columns)
                    {
                        string column = dcTemp.ColumnName;
                        if (dwTemp.Table.Columns.Contains(column.Trim()))
                        {
                            dwABINFO[column] = dwTemp[column.Trim()];
                        }
                    }
                    if (flgNew)
                    {
                        flgNew = false;
                        setANINFOR.Tables[0].Rows.Add(dwABINFO);
                    }
                    //setANINFOR.Merge(setUI.Tables["ABPOS"]);
                }
            }
            if (setANINFOR.Tables.Contains("GAAUF"))
            {
                setANINFOR.Tables["GAAUF"].Clear();
            }
            if (setUI.Tables.Contains("GAAUF"))
            {
                setANINFOR.Merge(setUI.Tables["GAAUF"]);
            } */
            m_DataX = new AB45AV95Data(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt = new AB45AV95Report(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = PrintType;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
